﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Reflection;
using System.Net.Cache;
using log4net;
using Newtonsoft.Json.Linq;

namespace ZuggerWpf
{
    public class WebTools
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
       
        public static string Download(string url)
        {
            string json = string.Empty;           

            WebClient client = new WebClient();
            try
            {
                json = client.DownloadString(url);
            }
            catch (Exception exp)
            {
                logger.Error(string.Format("发送服务器请求错误:{0}", exp.ToString()));
            }
            finally
            {
                client.Dispose();
            }

            return json;
        }

        public static JObject Post(string url, JObject json_data, JObject header)
        {
            try
            {
                string post_data = string.Empty;
                if (json_data != null)
                {
                    post_data = json_data.ToString(Newtonsoft.Json.Formatting.None);
                }
                byte[] byteArray = Encoding.UTF8.GetBytes(post_data);

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
                webRequest.Method = "POST";
                webRequest.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                webRequest.ContentLength = byteArray.Length;

                if (header != null)
                {
                    foreach (var item in header.Properties())
                    {
                        webRequest.Headers.Add((string)item.Name, (string)item.Value);
                    }
                }

                using (Stream newStream = webRequest.GetRequestStream())
                {
                    newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
                }

                using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        string res_content = sr.ReadToEnd();
                        JObject res = JObject.Parse(res_content);
                        return res;
                    }
                }
            }
            catch {
                return null;
            }
            
        }

        public static JObject Get(string url, JObject header)
        {
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
                webRequest.Method = "GET";
                webRequest.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";

                if (header != null)
                {
                    foreach (var item in header.Properties())
                    {
                        webRequest.Headers.Add((string)item.Name, (string)item.Value);
                    }
                }

                using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        string res_content = sr.ReadToEnd();
                        JObject res = JObject.Parse(res_content);
                        return res;
                    }
                }
            }
            catch {
                return null;
            }
        }
    }
}
